package com.allwees.support.fileservice.upload;

import lombok.Getter;
import lombok.ToString;

import java.util.Arrays;

/**
 * alioss bucket映射枚举
 *
 * <p>
 * value为四位,前两位为bucket的code,后两位为文件夹code
 * bucketDir 中通过/符号区分bucket和文件夹,/前面的为bucket,后面的为文件夹
 *
 * @author mouhn
 * @since 2019-09-18
 */
@Getter
@ToString
public enum AliossBucketEnum {


    USER_AVATAR("0101", "bs-common", "avatar"),
    USER_FEEDBACK("0102", "bs-common", "feedback"),

    ORDER_REVIEW("0120", "bs-common", "review"),
    ORDER_REFUND("0121", "bs-common", "refund"),


    SPU_IMG_M("0201", "prod-files", "m", true),

    SUPPLIER_CARD("0151", "bs-common", "s-card"),
    SUPPLIER_BRAND_LICENSE("0152", "bs-common", "s-brandlicense"),

    SYS_BANNER("0161", "bs-common", "banner"),

    //仅测试
    TEST("90", "bs-test2"),

    ;

    private String value;
    private String bucket;
    private String dir;
    private boolean user;

    AliossBucketEnum(String value, String bucket) {
        this.value = value;
        this.bucket = bucket;
    }


    AliossBucketEnum(String value, String bucket, String dir) {
        this(value, bucket);
        this.dir = dir;
    }

    AliossBucketEnum(String value, String bucket, String dir, boolean user) {
        this(value, bucket, dir);
        this.user = user;
    }

    public String getDir() {
        return dir != null ? dir : "";
    }

    public static String getBucket(String value) {
        AliossBucketEnum e = getEnum(value);
        return e != null ? e.getBucket() : "";
    }

    public static String getDir(String value) {
        AliossBucketEnum e = getEnum(value);
        return e != null ? e.getDir() : "";
    }

    public static AliossBucketEnum getEnum(String value) {
        AliossBucketEnum[] enums = AliossBucketEnum.values();
        return Arrays.stream(enums).filter(e -> value.equals(e.getValue())).findFirst().orElse(null);
    }

    public static AliossBucketEnum getEnum(String bucket, String dir) {
        String dir1 = dir != null ? dir : "";

        AliossBucketEnum[] enums = AliossBucketEnum.values();
        return Arrays.stream(enums).filter(
                e -> bucket.equals(e.getBucket()) && dir1.equals(e.getDir())
        ).findFirst().orElse(null);
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }
}
